<?php

namespace services\notices;

use think\Request;
use think\Response;
use Carbon\Carbon;
use think\Session;
use think\Exception;
use think\exception\ClassNotFoundException;
use EasyWeChat\Foundation\Application;

class Notice
{
    protected static $instance = null;

    public $options = [
        'app_id'  => 'wx5a1090e16a2f3e95',
        'accessKey' => 'gw9QdAPaIsYjR1Uu', 
        'accessSecret' => 'dyOOXvGW0Q6bG05Agr7hXJCr5wdBDd', 
        'accountName' => 'hy@m.shsytour.cn', 
        'fromAlias' => '双辉'
    ];

    protected $providers = [
        Wx::class,
        Mail::class,
        System::class
    ];

    /**
     * @param array $options
     * @return static
     */
    public static function instance($options = [])
    {
        if (is_null(self::$instance)) {
            self::$instance = new self($options);
        }
        return self::$instance;
    }

    protected function __construct($options = [])
    {
        if ($options) {
            $this->options = array_merge($this->options, $options);
        }

        foreach ($this->providers as $provider) {
            new $provider($this);
        }
    }

    protected function Transformation($type)
    {
        if ($type == 'sign') {
            return 'system';
        }
        return $type;
    }

    public function send($user_info, $template, $data, $other = null)
    {
        try {
            foreach ($user_info as $type => $recipients) {
                $type = $this->Transformation($type);
                $this->$type->send($recipients, $template, $data, $other);
            }

        } catch (\Exception $e) {}

        return true;
    }
}
